package com.bawei.module_main.ui.guide

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_image.GlideApp
import com.bawei.lib_storage.mmkv.AppMMKV
import com.bawei.module_main.R
import com.bawei.module_main.databinding.ActivityGuideBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.Ui.GuideActivity)
class GuideActivity : BaseActivity<BaseViewModel, ActivityGuideBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_guide
    override val mViewModel: BaseViewModel by viewModels()

    override fun initView() {

    }

    override fun initListener() {
        mBinding.btnEnter.setOnClickListener {
            AppMMKV.needGuide = false
            ARouter.getInstance()
                .build(Router.Ui.MainActivity)
                .withTransition(R.anim.right_in, R.anim.left_out)
                .navigation(this)
            ActivityCompat.finishAffinity(this)
        }
    }

    override fun initData() {
        mBinding.vp.adapter = GuidePageAdapter()
        mBinding.vp.offscreenPageLimit = 1
        mBinding.vp.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                // mBinding.vIndicator.translationX = 48.dp * (positionOffset + position)
                when (position) {
                    0 -> {
                        mBinding.guide1.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_checked)
                        mBinding.guide2.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_uncheck)
                        mBinding.guide3.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_uncheck)
                    }
                    1 -> {
                        mBinding.guide1.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_uncheck)
                        mBinding.guide2.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_checked)
                        mBinding.guide3.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_uncheck)
                    }
                    2 -> {
                        mBinding.guide1.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_uncheck)
                        mBinding.guide2.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_uncheck)
                        mBinding.guide3.background = ContextCompat.getDrawable(this@GuideActivity, R.drawable.bg_indicator_checked)
                    }

                }
            }

            override fun onPageSelected(position: Int) {
                if (position == 2) {
                    hideIndicator()
                } else {
                    showIndicator()
                }
            }
        })
    }

    private fun hideIndicator() {
        mBinding.lGuide.visibility = View.INVISIBLE
        mBinding.btnEnter.visibility = View.VISIBLE
    }

    private fun showIndicator() {
        mBinding.lGuide.visibility = View.VISIBLE
        mBinding.btnEnter.visibility = View.INVISIBLE
    }

    private inner class GuidePageAdapter:RecyclerView.Adapter<RecyclerView.ViewHolder>(){

        private val guidePicRes = listOf(R.mipmap.bg_guide_1, R.mipmap.bg_guide_2, R.mipmap.bg_guide_3)
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return object : RecyclerView.ViewHolder(ImageView(parent.context).apply {
                scaleType = ImageView.ScaleType.CENTER_CROP
                layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            }) {}
        }

        override fun getItemCount(): Int {
            return guidePicRes.size
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            //按屏幕比例，截取上部分的图片
            val imageView: ImageView = holder.itemView as ImageView
            GlideApp.with(imageView.context)
                .load(guidePicRes[position])
                .into(imageView)
        }

    }

}